package cn.piaoyi.web.action;

import java.util.List;
import cn.piaoyi.bean.Payment;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.PaymentService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AdminPayment extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminPayment.class);
	
	private PaymentService paymentService;

	public PaymentService getPaymentService() {
		return paymentService;
	}

	public void setPaymentService(PaymentService paymentService) {
		this.paymentService = paymentService;
	}


	private long id;
	private String name;
	private String code;
	private String explains;
	private String config;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getExplains() {
		return explains;
	}

	public void setExplains(String explains) {
		this.explains = explains;
	}

	public String getConfig() {
		return config;
	}

	public void setConfig(String config) {
		this.config = config;
	}
	
	private List paymentList;

	public List getPaymentList() {
		return paymentList;
	}

	public void setPaymentList(List paymentList) {
		this.paymentList = paymentList;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		List l = this.getPaymentService().findPayments("id", 0);
		this.setPaymentList(l);
		return SUCCESS;
	}
	
	public String add(){
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		this.name = StringUtils.trimToEmpty(this.name);
		this.code = StringUtils.trimToEmpty(this.code);
		this.explains = StringUtils.trimToEmpty(this.explains); 
		this.config = StringUtils.trimToEmpty(this.config); 
		//添加支付方式
		Payment p = new Payment();
		p.setName(this.name);
		p.setCode(this.code);
		p.setExplains(this.explains);
		p.setConfig(this.config);
		try {
			this.getPaymentService().createPayment(p);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		this.setAction("doupdate");
		Payment p = this.getPaymentService().findPaymentById(this.id);
		this.setName(p.getName());
		this.setCode(p.getCode());
		this.setExplains(p.getExplains());
		this.setConfig(p.getConfig());
		return INPUT;
	}
	
	public String doupdate(){
		this.name = StringUtils.trimToEmpty(this.name);
		this.code = StringUtils.trimToEmpty(this.code);
		this.explains = StringUtils.trimToEmpty(this.explains); 
		this.config = StringUtils.trimToEmpty(this.config); 

		Payment p = this.getPaymentService().findPaymentById(this.id);
		p.setName(this.name);
		p.setCode(this.code);
		p.setExplains(this.explains);
		p.setConfig(this.config);
		try {
			this.getPaymentService().updatePayment(p);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getPaymentService().removePaymentById(this.id);
		return "index";
	}



}
